{% load admin_static %}{% load suit_tags %}{% load url from suit_compat %}<!DOCTYPE html>
<html lang="{{ LANGUAGE_CODE|default:"en-us" }}" {% if LANGUAGE_BIDI %}dir="rtl"{% endif %}>
<head>
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>{% block title %}{{ title }} | {{ 'ADMIN_NAME'|suit_conf }}{% endblock %}</title>
  {% block before_suit_styles %}{% endblock %}
  <link rel="stylesheet" type="text/css" href="{% block stylesheet %}{% endblock %}"/>
  <link rel="stylesheet" type="text/css" href="{% static 'suit/bootstrap/css/bootstrap.min.css' %}" media="all"/>
  <link rel="stylesheet" type="text/css" href="{% static 'suit/css/suit.css' %}" media="all">
  {% block extrastyle %}{% endblock %}
  {% if LANGUAGE_BIDI %}<link rel="stylesheet" type="text/css" href="{% block stylesheet_rtl %}{% static "admin/css/rtl.css" %}{% endblock %}"/>{% endif %}
  <script type="text/javascript">window.__admin_media_prefix__ = "{% filter escapejs %}{% static "admin/" %}{% endfilter %}";</script>
  {% block suit_jquery %}{% if '1.9'|django_version_gte %}<script src="{% static 'suit/js/jquery-2.2.4.min.js' %}">{% else %}<script src="{% static 'suit/js/jquery-1.8.3.min.js' %}">{% endif %}</script>{% endblock %}
  <script type="text/javascript">var Suit = { $: $.noConflict() }; if (!$) $ = Suit.$; </script>
  <script src="{% static 'suit/bootstrap/js/bootstrap.min.js' %}"></script>
  {% if 'SHOW_REQUIRED_ASTERISK'|suit_conf %}
  <style type="text/css">.required:after { content: '*'; margin: 0 0 0 5px; position: absolute; color: #ccc;}</style>
  {% endif %}
  {% block extrahead %}
    <script src="https://www.promisejs.org/polyfills/promise-6.1.0.js"></script>
    <script type="text/javascript" src="https://gosspublic.alicdn.com/aliyun-oss-sdk.min.js"></script>
    <style>
        .progress {
            width: 38px;
        }
    </style>
  <script>
      Date.prototype.format = function(fmt) {
         var o = {
            "M+" : this.getMonth()+1,                 //月份
            "d+" : this.getDate(),                    //日
            "h+" : this.getHours(),                   //小时
            "m+" : this.getMinutes(),                 //分
            "s+" : this.getSeconds(),                 //秒
            "q+" : Math.floor((this.getMonth()+3)/3), //季度
            "S"  : this.getMilliseconds()             //毫秒
        };
        if(/(y+)/.test(fmt)) {
                fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
        }
         for(var k in o) {
            if(new RegExp("("+ k +")").test(fmt)){
                 fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
             }
         }
        return fmt;
    }

      var p = window.location.protocol;
      var h = window.location.host;
      var app_server_endpoint = p+'//'+h;
    var appServer = app_server_endpoint + '/adm/aliyun/oss/sts/upload/token/for/admin';
    var region = 'oss-cn-beijing';

    var urllib = OSS.urllib;
    var Buffer = OSS.Buffer;
    var OSS = OSS.Wrapper;
    var STS = OSS.STS;

    var applyTokenDo = function (func, file_id, control_id, field_id) {
      var url = appServer;
      return urllib.request(url, {
        method: 'GET'
      }).then(function (result) {
          console.log(result.data.toString());
        var data = JSON.parse(result.data);
        document.getElementById('cdn_endpoint').value = data.oss_cdn_endpoint;
        var creds = data.Credentials;
        var client = new OSS({
          region: region,
          accessKeyId: creds.AccessKeyId,
          accessKeySecret: creds.AccessKeySecret,
          stsToken: creds.SecurityToken,
          bucket: data.bucket,
        });
        return func(client, file_id, control_id, field_id);
      });
    };

    var uploadFile = function (client, file_id, control_id, field_id) {
        var oss_cdn_endpoint = document.getElementById('cdn_endpoint').value;
      var file = document.getElementById(file_id).files[0];
      var file_name_parts = file.name.split('.');
      var ext = file_name_parts[file_name_parts.length-1];
      var key = 'adm/'+new Date().format('yyyy/MM/dd/hh/')+Math.random().toString(23).substr(2)+'.'+ext;

      return client.multipartUpload(key, file, {
        progress: function (p) {
          return function (done) {
            var bar = document.getElementById(field_id+'--progress-bar');
            bar.style.width = Math.floor(p * 100) + '%';
            bar.innerHTML = Math.floor(p * 100) + '%';
            done();
          }
        }
      }).then(function (res) {
          var access_url = oss_cdn_endpoint + '/' + res.name;
        console.log(access_url);
        document.getElementById(control_id).value = access_url;
        document.getElementById(field_id+'--preview-link').href = access_url;
      });
    };

    window.onload = function () {
        var btns = document.getElementsByClassName('ub');
        for (var i=0; i<btns.length; i++){
            btns[i].onclick = function () {
              var btn_eid = this.id;
              var field_id = btn_eid.split('--')[0];
              var control_id = 'id_'+field_id;
              var file_input_id = field_id+'--'+'file-input';
              console.log(control_id);

            applyTokenDo(uploadFile, file_input_id, control_id, field_id);
          }
        }
    };
    </script>
    {% endblock %}
  {% block blockbots %}
    <meta name="robots" content="NONE,NOARCHIVE"/>{% endblock %}
</head>
{% load i18n %}

<body class="{% if is_popup %}popup {% endif %}{% block bodyclass %}{% endblock %}">

<!-- Sticky footer wrap -->
<div id="wrap">

  <!-- Container -->
  {% block container %}
    <div id="container">

      {% block header %}
        {% if not is_popup %}
          <!-- Header -->
          <div id="header" class="header">
            <div id="branding">
              <a href="{% url 'admin:index' %}"><h1 id="site-name">{% block branding %}{{ 'ADMIN_NAME'|suit_conf }}{% endblock %}</h1></a>
            </div>

            {% block header_time %}
            <div class="header-content header-content-first">
              <div class="header-column icon">
                <i class="icon-time"></i>
              </div>
              <div class="header-column">
                <span class="date"> {% suit_date %}</span><br>
                <span class="time" id="clock">{% suit_time %}</span>
              </div>
            </div>
            {% endblock %}

            {% block header_content %}
              <!--<div class="header-content">
                <div class="header-column icon">
                  <i class="icon-comment"></i>
                </div>
                <div class="header-column">
                  <a href="" class="grey"><b>2</b> new messages</a>
                </div>
              </div>-->
            {% endblock %}

            {% if user.is_active and user.is_staff %}
              <div id="user-tools">
                {% block welcome-msg %}
                  {% trans 'Welcome,' %}
                  <strong>
                    {% filter force_escape %}
                      {% firstof user.first_name user.username %}{% endfilter %}</strong>.
                {% endblock %}
                <span class="user-links">
                {% block userlinks %}
                  {% url 'django-admindocs-docroot' as docsroot %}
                  {% if docsroot %}
                    <a href="{{ docsroot }}">{% trans 'Documentation' %}</a>
                    <span class="separator">|</span>
                  {% endif %}
                  <a href="{% url 'admin:password_change' %}">{% trans 'Change password' %}</a>
                  <span class="separator">|</span>
                  <a href="{% url 'admin:logout' %}">{% trans 'Log out' %}</a>
                {% endblock %}
                </span>
              </div>
            {% endif %}
            {% block nav-global %}{% endblock %}
          </div>
        {% endif %}
        <!-- END Header -->
      {% endblock %}


      <div class="suit-columns {{ is_popup|yesno:'one-column,two-columns' }}">

        {% block content-center %}
          <div id="suit-center" class="suit-column">

            {% if not is_popup %}
              {% block breadcrumbs %}
                <ul class="breadcrumb">
                  <li><a href="{% url 'admin:index' %}">{% trans 'Home' %}</a>
                    {% if title %}
                      <span class="divider">&raquo;</span>
                      </li>
                      <li class="active">
                      {{ title }}
                    {% endif %}
                    </li>
                </ul>
              {% endblock %}
            {% endif %}

            {% block messages %}
              {% if messages %}

                {% for message in messages %}
                  <div class="alert alert-{% firstof message.tags 'info' %}">
                    <button class="close" data-dismiss="alert">×</button>
                    {{ message }}
                  </div>
                {% endfor %}
              {% endif %}
            {% endblock messages %}

            <!-- Content -->
            <div id="content" class="{% block coltype %}colM{% endblock %} row-fluid">
              {% block pretitle %}{% endblock %}
              {% block content_title %}{% if title %}
                <h2 class="content-title">{{ title }}</h2>
              {% endif %}{% endblock %}
              {% block content %}
                <input type="hidden" id="cdn_endpoint" />
                {% block object-tools %}{% endblock %}
                {{ content }}
              {% endblock %}
              {% block sidebar_content %}
                {% block sidebar %}{% endblock %}
              {% endblock %}
            </div>
            <!-- END Content -->
          </div>
        {% endblock %}


        {% block content-left %}
          {% if not is_popup %}
            <div id="suit-left" class="suit-column">
              {% block quick-search %}
                {% with 'SEARCH_URL'|suit_conf as search_url %}
                  {% if search_url %}
                    <form class="form-search nav-quick-search" autocomplete="off" action="{% if '/' in search_url %}{{ search_url }}{% else %}{% url search_url %}{% endif %}" method="GET">
                      <input type="text" name="q" class="input-medium search-query" id="quick-search">
                      <i class="input-icon icon-search"></i>
                      <input type="submit" class="submit" value="">
                    </form>
                  {% endif %}
                {% endwith %}
              {% endblock %}

              {% include 'suit/menu.html' %}

            </div>
          {% endif %}
        {% endblock %}

      </div>
    </div>
  {% endblock %}

  {% if not is_popup %}
  <!-- Sticky footer push -->
  <div id="push"></div>
  {% endif %}

</div>

{% block footer %}
  {% if not is_popup %}
  <div id="footer" class="footer">
    <div class="content">
      <div class="tools">
        {% block footer_links %}
          <!--<a href="http://djangosuit.com/support/" target="_blank" class="icon"><i class="icon-question-sign"></i>Support</a>-->
          <!--<a href="http://djangosuit.com/pricing/" target="_blank" class="icon"><i class="icon-bookmark"></i>Licence</a>-->
          <!--<a href="http://github.com/darklow/django-suit/issues" target="_blank" class="icon"><i class="icon-comment"></i>Report a bug</a>-->
        {% endblock %}
      </div>

      <div class="copyright">
        {% block copyright %}
          <!--Copyright &copy; 2013-{% now "Y" %} DjangoSuit.com<br>Developed by <a href="http://djangosuit.com" target="_blank">DjangoSuit.com</a>-->
        {% endblock %}
      </div>

      <div class="branding">{% block footer_branding %}
        {% with 'ADMIN_NAME'|suit_conf as admin_name %}
          {{ admin_name }}
          {% if admin_name == 'Django Suit' %}
            v{{ 'VERSION'|suit_conf }}
          {% endif %}
        {% endwith %}
      {% endblock %}</div>
    </div>
  </div>
  {% endif %}
{% endblock %}

  <script src="{% static 'suit/js/suit.js' %}"></script>
  {% block extrajs %}{% endblock %}

</body>
</html>
